home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / RTF / search_path.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  121 lines

  1. //  search_path.h - 
  2.  
  3. // Copyright (c) 1990, 1991, 1992 Convex Computer Corporation
  4. // All rights reserved.
  5.  
  6. /**********************************************************************
  7. ** File:    search_path.h
  8. ** Ident:    $CHeader: search_path.h 1.1 92/06/11 18:44:51 $
  9. ** Language:    C++
  10. ** Purpose:
  11.     
  12. ** Types
  13.     None
  14. ** Constants:
  15.  
  16. ** Global Data:
  17.  
  18. ** Static Data:
  19.  
  20. ** Global Functions: 
  21.  
  22. ** Public methods:
  23.  
  24. ** Private methods:
  25.  
  26. ** Static Public methods:
  27.  
  28. ** Static Private methods:
  29.  
  30. ** Notes:
  31.  
  32. ** Revision History:
  33. ** Who    Date     Description
  34.    ---  -------- ----------------------------------------------------
  35.    DWC  1-31-92   initial implementation   
  36. **********************************************************************/
  37.  
  38. /* for F_OK, etc. */
  39. #include <unistd.h>
  40. /* for FILE */
  41. #include <stdio.h>
  42.  
  43. class SearchPath{
  44. public:
  45.   typedef const char* Filename;
  46.   typedef const char* Path;
  47.   /* are these in an include file somewhere? */
  48.   enum{max_name_len = 255, max_path_len = 1023};
  49.  
  50.   SearchPath(Path p = 0);
  51.   ~SearchPath();
  52.  
  53.   char* lookup(Filename, int mode, const char* extensions=0);
  54.   /* USE : real_path = sp.lookup("filename", R_OK, ":.Z");
  55.    *       delete real_path
  56.    * ERRS: return 0
  57.    */
  58.  
  59.   FILE* open(Filename, const char* extensions=0);
  60.   /* USE : fp = sp.open("filename", ":.Z")
  61.    *       to read from filename
  62.    */
  63.  
  64.   int add_directories(Path);
  65.   /* USE : total = sp.add_directories("/here:/there:other/place");
  66.    */
  67.  
  68.   Filename first_dir();
  69.   Filename next_dir();
  70.   /* USE : for(d = sp.first_dir(); d; d = sp.next_dir())
  71.    *          do stuff with d
  72.    */
  73.  
  74. private:
  75.   static char buffer[max_path_len+1];
  76.   static int len;
  77.  
  78.   Filename* dirs;
  79.   int    dir_qty;
  80.   Filename*  current;
  81.   int    allocated;
  82.  
  83.   void grow(int);
  84.   /* USE: grow(to_new_size);
  85.    */
  86.  
  87.   Filename find(Filename, int mode, const char* extensions=0);
  88. };
  89.  
  90. inline SearchPath::SearchPath(Path path){
  91.   dirs = 0;
  92.   allocated = -1;
  93.   dir_qty = 0;
  94.   current = 0;
  95.   add_directories(path);
  96. }
  97.  
  98. inline SearchPath::~SearchPath()
  99. {
  100.   delete [] dirs;
  101. }
  102.  
  103. inline SearchPath::Filename
  104. SearchPath::first_dir()
  105. {
  106.   current = dirs;
  107.   return current ? *current : 0;
  108. }
  109.  
  110. inline SearchPath::Filename
  111. SearchPath::next_dir()
  112. {
  113.   if(!current)
  114.     return first_dir();
  115.   else
  116.     return (++current - dirs <dir_qty) ? *current : 0;
  117. }
  118.  
  119.  
  120.  
  121.